package MyFile;

import java.io.File;
import java.util.Scanner;

public class demo5 {
    //删除指定的文件,给定指定的目录与删除的文件名
    private static int sign = 0;
    public static void main(String[] args) {
        delete("D:\\software","tempText24_4_18.txt");
        if (sign == 0){
            System.out.println("没找到文件，删除失败");
        }
    }
    private  static Scanner scanner = new Scanner(System.in);
    public static void delete(String src,String name){
        File file = new File(src);

        if ( !file.exists()){
            System.out.println("文件不存在");
            return;
        }
        if (file.isFile()){
            System.out.println("不是目录文件");
            return;
        }

        File[] files = file.listFiles();
        if (files == null){
            return;
        }
        if (files.length == 0){
            return;
        }
        File temp;
        String contain;
        for (int i = 0; i < files.length; i++) {
            temp = files[i];
            if (temp.isFile()){
                if (temp.getName().equals(name)){
                    System.out.println("找到该文件了 ==>" + temp.getAbsoluteFile());
                    System.out.println("确定要删除该文件嘛？yes/no");
                    contain = scanner.next();
                    if (contain.contains("yes")){
                        temp.delete();
                        System.out.println("删除成功!!!");
                        sign = 1;
                        return;
                    }
                }
            }else {
                delete(temp.getAbsolutePath(), name);
            }
        }

    }
}
